<template>
  <div class="login_bg">
    <div class="login_img">
      <img class="login_logo" src="@/assets/login/login_logo.png" alt="" />
      <img class="login_icon" src="@/assets/login/login_icon.png" alt="" />
    </div>

    <div class="login">
      <p class="title">{{ identifyList[index] }}端登录</p>

      <!-- 输入表单组件 status-icon显示校验反馈图标 rules绑定校验规则 :label-position表单对齐方向 -->
      <el-form :model="loginUser" status-icon :rules="rules" :label-position="'left'" ref="loginUser" label-width="100px">
        <el-form-item label="账号" prop="id">
          <el-input type="text" v-model="loginUser.id" autocomplete="off" />
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input type="text" v-model="loginUser.password" autocomplete="off" show-password />
        </el-form-item>
      </el-form>

      <el-link class="find_pd" :underline="false" type="danger" @click="$router.push('/find')">忘记密码</el-link>
      <el-link class="login_identify" :underline="false" @click="changeList">我是{{ identifyList[index] }}</el-link>
      <el-button class="login_bt" type="primary" plain @click="submitForm('loginUser')">登录</el-button>
    </div>

    <footer>Copyright © 2023.广应科你这瓜保熟吗</footer>
  </div>
</template>

<script>
import { login } from '@/api/login'
import { checkInfo } from '@/utils/storage'

export default {
  name: 'LoginIndexPage', // 登录页面
  data () {
    return {
      identifyList: ['教师', '教务', '学生'], // 存放身份的数组
      index: 0, // 用于标识identifyList的下标
      // 存放登录用户的信息
      loginUser: {
        id: '',
        password: ''
      },
      // 设置对应的校验规则
      rules: {
        id: [
          { required: true, message: '请输入账号', trigger: 'blur' },
          { pattern: /^\d{12}$/, message: '账号必须为12位数字', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入新密码', trigger: 'blur' },
          { pattern: /^\d{6}$/, message: '密码必须为6位数字', trigger: 'blur' }
        ]
      }
    }
  },
  created () {
    checkInfo() // 进入页面前检查localStorage下是否存在登录信息
  },
  methods: {
    // 切换身份
    changeList () {
      this.index++
      if (this.index >= this.identifyList.length) this.index = 0
    },
    // 提交表单 发送请求 登录
    submitForm (loginUser) {
      // 登录前校验账号密码是否合法
      this.$refs[loginUser].validate(async (valid) => {
        if (valid) {
          const { data: { data } } = await login(this.loginUser, this.identifyList[this.index])
          this.$store.commit('user/setUserInfo', data) // 返回的data内含有token name等的信息 需要存入本地

          // 不同身份登录跳转的页面不一样
          if (this.identifyList[this.index] === '教师') this.$router.push('/teacherIndex')
          if (this.identifyList[this.index] === '教务') this.$router.push('/adminIndex')
          if (this.identifyList[this.index] === '学生') this.$router.push('/studentIndex')
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.login_bg {
  position: relative;
  width: 100%;
  height: 100%;
  background: url('/src/assets/login/login_bg.jpg') no-repeat;
  background-size: 100% 100%;
  background-position: center 0px;
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 1;

  .login_img {
    width: 30%;
    z-index: 3;

    .login_logo {
      display: block;
      width: 100%;
      height: auto;
    }

    .login_icon {
      margin-top: 10%;
      margin-left: 10%;
      display: block;
      width: 100%;
      height: auto;
    }
  }

  .login {
    width: 365px;
    height: auto;
    margin-left: 10%;
    padding: 2%;
    background: #fff;
    border-radius: 5px;
    box-shadow: 0 0 5px rgba(0, 0, 0, 0.3);

    .title {
      margin: 0px auto 20px auto;
      text-align: left;
      font-size: 30px;
      font-weight: bolder;
      color: #3c3c3c;
    }

    .find_pd {
      float: left;
      font-size: medium;
      font-weight: bold;
      margin: 10px 15px;
    }

    .login_identify {
      float: right;
      font-size: medium;
      font-weight: bold;
      margin: 10px 15px;
    }

    .login_bt {
      width: 100%;
      margin-top: 10px;
      float: right;
    }
  }

  footer {
    position: absolute;
    bottom: 4%;
    width: 100%;
    text-align: center;
  }
}
</style>
